home *** CD-ROM | disk | FTP | other *** search
- /* ARRAYS.CPP 1.01 DEMONSTRATION OF C LANGUAGE ARRAY DIFFICULTIES
-
- V01 L01 1991-10-29-11:48 make C++ version to see if any difference.
-
- L00 1991-10-29-11:13 develop array case based on problem raised
- by Barry Gombert. How do you put a pointer to an array in
- a struct definition?
-
- */
-
- #include <assert.h>
- #include <stdlib.h>
-
- struct IBlock
-
- {unsigned long (*Band)[];};
-
- struct JBlock
-
- {unsigned long *BandArray;};
-
-
- int main(int argc, char *argv[])
-
- { /* figure out some Band array information */
-
-
- unsigned long Band1[10];
-
- IBlock Block1;
-
- JBlock Block2;
-
- int i;
-
- i = sizeof(Band1)/sizeof(Band1[0]);
- while (i--)
- {Band1[i] = ((unsigned long)(1) << 16) + i; }
-
- Block1.Band = Band1;
- // The rhs type really is incompatible!
-
- Block1.Band = (unsigned long (*)[]) Band1;
- // Works a charm, as usual.
- Block1.Band = new (unsigned long [10]);
- // The same problem!
-
- Block1.Band = (unsigned long (*)[]) new (unsigned long [10]);
- // The same solution ...
-
- assert(Block1.Band == Band1);
-
- assert(Block1.Band == (unsigned long (*)[]) Band1);
-
- assert(Block1.Band[0] == Band1[0]);
- // An interesting variation on this problem.
-
- assert((*Block1.Band)[0] == Band1[0]);
- // Feel better now?
-
- Block2.BandArray = Band1;
- // The rhs type really is compatible with
- // *this* lhs though! So now we know what
- // the value of (Band1) is, aye?
-
- assert(Block2.BandArray == Band1);
-
- assert(Block2.BandArray[0] == Band1[0]);
-
- return 0;}
-